
/*ajimber */

package vista.Vehiculo;

import java.util.ArrayList;
import java.util.List;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableModel;
import modelo.entidades.Vehiculo;

public class VehiculoTableModel implements TableModel {

    private List<Vehiculo> Vehiculos = new ArrayList<Vehiculo>();
    private List<TableModelListener> tableListeners = new ArrayList<TableModelListener>();
    //private List<ListDataListener> dataListeners = new ArrayList<ListDataListener>();
    private static VehiculoTableModel model = null;

    private VehiculoTableModel() {
    }

    public static VehiculoTableModel create() {
        if (model == null) {
            model = new VehiculoTableModel();
        }
        return model;
    }

    public List<Vehiculo> getVehiculos() {


        return Vehiculos;

    }

    public void setVehiculos(List<Vehiculo> Vehiculos) {
        this.Vehiculos = Vehiculos;
        fireContentsChanged();
    }

    protected void fireContentsChanged() {
        fireContentsChangedTableModel();
    }
    //--------------- MÉTODOS PROPIOS DE TableModel -------------------------

    public Vehiculo getRow(int indexRow) {
        return Vehiculos.get(indexRow);
    }

    @Override
    public int getRowCount() {
        return Vehiculos.size();
    }

    @Override
    public int getColumnCount() {
        return 3; //ya que tendremos tres valores a mostrar por contacto
    }

    @Override
    public String getColumnName(int columnIndex) {
        String name = null;
        switch (columnIndex) {
            case 0:
                name = "Matrícula";
                break;
            case 1:
                name = "Usuario";
                break;
            case 2:
                name = "Grupo";

                break;
        }
        return name;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return String.class; //Todos los valores de contacto son un String
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false; //No permito editar celdas
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Vehiculo vehiculo = Vehiculos.get(rowIndex);
        String value = null;
        switch (columnIndex) {
            case 0:
                value = vehiculo.getMatricula();
                break;
            case 1:
                value = vehiculo.getUsuario();
                break;
            case 2:
                value = vehiculo.getGrupo();
                break;
        }
        return value;

    }

    @Override
    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void addTableModelListener(TableModelListener l) {
        tableListeners.add(l);
    }

    @Override
    public void removeTableModelListener(TableModelListener l) {
        tableListeners.remove(l);
    }

    protected void fireContentsChangedTableModel() {
        //TableModelEvent event = new TableModelEvent(this,0,getRowCount());
        TableModelEvent event = new TableModelEvent(this, 0, this.getRowCount() - 1, TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT);
        for (TableModelListener listener : tableListeners) {
            listener.tableChanged(event);
        }
    }
}
